package cn.fl.capital.utils;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.capital.vo.CapFundVO;
import cn.fl.capital.vo.CapFundVfDtlVO;
import cn.fl.capital.vo.CapOverdueInfoVO;
import cn.fl.capital.vo.CapPayInfoVO;
import cn.lb.framework.poi.excel.ExcelExportUtil;
import cn.lb.framework.poi.excel.entity.TemplateExportParams;
import cn.lb.framework.poi.excel.entity.enmus.ExcelStyleType;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Function: 导出PDF
 * Created by Administrator on 2016/8/29.
 */
public class ExportFundList {

	private static final Logger logger = LoggerFactory.getLogger(ExportFundList.class);

    /**
     * 导出分解单
     *
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportDecomPdf(Map<String, Object> map) {

        ByteArrayOutputStream baos = null;
        // 设置纸张大小
        Document document = new Document(PageSize.A4, 20, 20, 30, 30);
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);

        baos = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, baos);

            // 设置中文
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 9号字体加粗
            Font FontBold = new Font(bfChinese, 9, Font.BOLD);
            // 9号 字体正常
            Font FontDefault = new Font(bfChinese, 9, Font.NORMAL);
            Font FontChinese = new Font(bfChinese, 12, Font.BOLD);
            Font FontDefault1 = new Font(bfChinese, 8, Font.BOLD);
            // 打开 document
            document.open();

            CapFundVO vo = (CapFundVO) map.get("CapFund");

            // 设置列宽
            float[] widthsPPC5 = {100f};
            PdfPTable tPdfPTable = new PdfPTable(widthsPPC5);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.addCell(makeCellWithNoBorder("  ", PdfPCell.ALIGN_RIGHT, FontDefault1));
            tPdfPTable.addCell(makeCellWithNoBorder("来款合同分解单", PdfPCell.ALIGN_CENTER, FontChinese));

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
            String datetime = simpleDateFormat.format(new Date());
            tPdfPTable.addCell(makeCellWithNoBorder(datetime, PdfPCell.ALIGN_CENTER, FontDefault));
            document.add(tPdfPTable);
            float[] widthsPPC = {1f, 0.5f};
            tPdfPTable = new PdfPTable(widthsPPC);
            tPdfPTable.setWidthPercentage(100f);

            // 头
            tPdfPTable.addCell(makeCellWithNoBorder("融资租赁公司来款合同分解单-分解单编号: ", PdfPCell.ALIGN_RIGHT, FontDefault));
            tPdfPTable.addCell(makeCellWithNoBorder(vo.getId() + "", PdfPCell.ALIGN_LEFT, FontDefault));
            document.add(tPdfPTable);

            //资金信息Table
            float[] widthsPPC1 = {0.1f, 0.2f, 0.1f, 0.2f, 0.1f, 0.2f};//六列
            tPdfPTable = new PdfPTable(widthsPPC1);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.addCell(makeCell("来款时间:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getAcceptDate() ? vo.getAcceptDates() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("来款单位：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getComeName() || !"".equals(vo.getComeName()) ? vo.getComeName() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("来款金额：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(nf.format(Double.valueOf(vo.getAmount()) / 100), PdfPCell.ALIGN_RIGHT, FontBold));

            tPdfPTable.addCell(makeCell("收款凭证：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getCertificateCd() ? vo.getCertificateCd() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("收款账号：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getAcceptCode() ? vo.getAcceptCode() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("收款类型：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getBorrowingLogoCd() ? vo.getBorrowingLogoCd() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("承租人：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getCstNm() ? vo.getCstNm() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("来款账号：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo.getComeCode() ? vo.getComeCode() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("分解金额：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(nf.format(Double.valueOf(vo.getUsedAmount()) / 100), PdfPCell.ALIGN_RIGHT, FontBold));

            Phrase objPhase = new Phrase(null != vo.getDocketRem() ? vo.getDocketRem() : "", FontBold);
            PdfPCell objCell = new PdfPCell(objPhase);
            objCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            objCell.setColspan(5);
            tPdfPTable.addCell(makeCell("来款备注：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(objCell);
            tPdfPTable.addCell(makeCell(" ", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(" ", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(" ", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(" ", PdfPCell.ALIGN_CENTER, FontBold));
            document.add(tPdfPTable);

            // 加空行
            document.add(new Paragraph("\n", FontDefault));

            //分解支付表明细Table
            float[] widthsPPC2 = {0.20f, 0.20f, 0.20f, 0.23f, 0.14f, 0.12f};
            tPdfPTable = new PdfPTable(widthsPPC2);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.setHeaderRows(1);
            tPdfPTable.addCell(makeCell("合同单位", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("开票单位", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("支付表编号", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("回款分解项目", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("分解时间", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("金额", PdfPCell.ALIGN_CENTER, FontBold));

            List<CapFundVfDtlVO> list = (List<CapFundVfDtlVO>) map.get("FundList");
            for (CapFundVfDtlVO fund : list) {
                tPdfPTable.addCell(makeCell(null != vo.getCstNm() ? vo.getCstNm() : "", PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell("", PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell(fund.getPaySchNo(), PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell(fund.getFeeTypCdNm(), PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell(DateUtils.formatDateToString(fund.getCrtTm(), "yyyy-MM-dd"), PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell(nf.format(Double.valueOf(fund.getVfAmt()) / 100), PdfPCell.ALIGN_CENTER, FontDefault));
            }
            document.add(tPdfPTable);

            //制表信息Table
            float[] widthsPPC3 = {0.2f, 0.3f, 0.2f, 0.3f};
            tPdfPTable = new PdfPTable(widthsPPC3);
            tPdfPTable.setWidthPercentage(80f);
            tPdfPTable.addCell(makeCellWithNoBorder("制表:", PdfPCell.ALIGN_LEFT, FontDefault));
            tPdfPTable.addCell(makeCellWithNoBorder(null != vo.getFundDecomUsrNm() ? vo.getFundDecomUsrNm() : "", PdfPCell.ALIGN_LEFT, FontDefault));
            tPdfPTable.addCell(makeCellWithNoBorder("审核:", PdfPCell.ALIGN_LEFT, FontDefault));
            tPdfPTable.addCell(makeCellWithNoBorder("", PdfPCell.ALIGN_LEFT, FontDefault));
            document.add(tPdfPTable);

        } catch (DocumentException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } finally {
            document.close();
        }
        return baos;
    }

    /**
     * 导出逾期
     *
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportOverDuePdf(Map<String, Object> map) {
        ByteArrayOutputStream baos = null;
        // 设置纸张大小
        Document document = new Document(PageSize.A4, 20, 20, 30, 30);
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);

        baos = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, baos);

            // 设置中文
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 9号字体加粗
            Font FontBold = new Font(bfChinese, 9, Font.BOLD);
            // 9号 字体正常
            Font FontDefault = new Font(bfChinese, 9, Font.NORMAL);
            Font FontChinese = new Font(bfChinese, 12, Font.BOLD);
            Font FontDefault1 = new Font(bfChinese, 8, Font.BOLD);
            // 打开 document
            document.open();

            float[] widthsPPC5 = {100f};
            PdfPTable tPdfPTable = new PdfPTable(widthsPPC5);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.addCell(makeCellWithNoBorder("  ", PdfPCell.ALIGN_RIGHT, FontDefault1));
            tPdfPTable.addCell(makeCellWithNoBorder("逾期催收", PdfPCell.ALIGN_CENTER, FontChinese));
            document.add(tPdfPTable);

            //分解支付表明细Table
            float[] widthsPPC2 = {0.15f, 0.15f, 0.15f, 0.12f, 0.14f, 0.12f, 0.14f, 0.12f};
            tPdfPTable = new PdfPTable(widthsPPC2);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.setHeaderRows(1);
            tPdfPTable.addCell(makeCell("承租人", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("逾期合同数", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("逾期支付表数", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("逾期天数", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("应付总金额", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("逾期总金额", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("罚息总金额", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("残值金额", PdfPCell.ALIGN_CENTER, FontBold));

            List<CapOverdueInfoVO> list = (List<CapOverdueInfoVO>) map.get("overdueList");
            String feeType = "";
            for (CapOverdueInfoVO vo : list) {
                tPdfPTable.addCell(makeCell(null != vo.getCstNm() ? vo.getCstNm() : "", PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell("" + vo.getContIds(), PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell("" + vo.getPayIds(), PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell("" + vo.getOverdueDay(), PdfPCell.ALIGN_CENTER, FontDefault));
                tPdfPTable.addCell(makeCell(nf.format(Double.valueOf(vo.getRecAmt()) / 100), PdfPCell.ALIGN_RIGHT, FontDefault));
                tPdfPTable.addCell(makeCell(nf.format(Double.valueOf(vo.getOverdueAmount()) / 100), PdfPCell.ALIGN_RIGHT, FontDefault));
                tPdfPTable.addCell(makeCell(nf.format(Double.valueOf(vo.getPenaltyAmount()) / 100), PdfPCell.ALIGN_RIGHT, FontDefault));
                tPdfPTable.addCell(makeCell("", PdfPCell.ALIGN_RIGHT, FontDefault));
            }
            document.add(tPdfPTable);
        } catch (DocumentException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } finally {
            document.close();
        }
        return baos;
    }

    /**
     * 导出逾期
     *
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportOverDueXls(Map<String, Object> map) {
        ByteArrayOutputStream baos = null;//¥
        List<CapOverdueInfoVO> list = (List<CapOverdueInfoVO>) map.get("overdueList");
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        for (CapOverdueInfoVO vo : list) {
            vo.setRecAmtStr(nf.format(Double.valueOf(vo.getRecAmt()) / 100));
            vo.setOverdueAmountStr(nf.format(Double.valueOf(vo.getOverdueAmount()) / 100));
            vo.setPenaltyAmountStr(nf.format(Double.valueOf(vo.getPenaltyAmount()) / 100));
        }
        map.put("overdueList",list);
        TemplateExportParams params = new TemplateExportParams(ExportFundList.class.getClassLoader().getResource("/template/capital/overdueTemp.xls").getFile(), false, "预期催收");
        params.setHeadingRows(2);
        params.setHeadingStartRow(2);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        params.setColForEach(true);//注：包含遍历list
        try {
            Workbook book = ExcelExportUtil.exportExcel(params, map);
            baos = new ByteArrayOutputStream();
            book.write(baos);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        return baos;
    }

    /**
     * 导出付款数据
     *
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportPayListXls(Map<String, Object> map) {
        ByteArrayOutputStream baos = null;//¥
        List<CapPayInfoVO> list = (List<CapPayInfoVO>) map.get("payList");
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        int sn=1;
        for (CapPayInfoVO vo : list) {
            vo.setSerialNum(sn++);
            vo.setPayAmtStr(nf.format(Double.valueOf(vo.getPayAmt()) / 100));
        }
        map.put("payList",list);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        TemplateExportParams params = new TemplateExportParams(
                ExportFundList.class.getClassLoader().getResource("/template/capital/payListTemp.xls").getFile(),
                false, "狮桥付款申请数据"+sdf.format(new Date()));
        params.setHeadingRows(1);
        params.setHeadingStartRow(1);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        params.setColForEach(true);//注：包含遍历list
        try {
            Workbook book = ExcelExportUtil.exportExcel(params, map);
            baos = new ByteArrayOutputStream();
            book.write(baos);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        return baos;
    }



    /**
     * 创建一般 单元格
     */
    private PdfPCell makeCell(String ocontentntent, int align, Font FontDefault) {
        Phrase objPhase = new Phrase(ocontentntent, FontDefault);
        PdfPCell objCell = new PdfPCell(objPhase);
        objCell.setHorizontalAlignment(align);

        return objCell;
    }

    /**
     * 创建 无边框 单元格
     */
    private PdfPCell makeCellWithNoBorder(String content, int align, Font FontDefault) {
        Phrase objPhase = new Phrase(content, FontDefault);
        PdfPCell objCell = new PdfPCell(objPhase);
        objCell.setHorizontalAlignment(align);
        objCell.setBorder(0);
        return objCell;
    }
}
